<!-- 管理员查看留言时先进入的“选择管理班级”页面 -->
<script setup lang="ts">
import { useLoadMore } from 'vue-request'
import { useUserInfoStore } from '@/store/userInfo'
import { fetchManagedClassListAPI } from '@/apis/fetchManagedClassList'

const router = useRouter()
const userInfoStore = useUserInfoStore()

// 分页列表数据
const { dataList, loading, loadingMore, noMore, loadMore } = useLoadMore<LoadMoreData<Class>>(
  fetchClassList,
  { isNoMore: d => d?.list.length === d?.total },
)

// “加载更多”的状态
const loadMoreStatus = computed(() => {
  if (loadingMore.value)
    return 'loading'
  else if (noMore.value)
    return 'nomore'
  else
    return 'loadmore'
})

onReachBottom(() => loadMore())

/**
 * @description 分页服务函数
 */
async function fetchClassList(d?: LoadMoreData<Class>): Promise<LoadMoreData<Class>> {
  // 当前页数
  const _page = d?.page ? d.page + 1 : 1

  const res = await fetchManagedClassListAPI({
    page: _page.toString(),
    limit: '10',
  }, userInfoStore.userInfo!.token!)

  return {
    list: res.data?.data.data || [],
    page: _page,
    total: res.data?.data.total || 9999,
  }
}

/**
 * @description 点击班级卡片的按钮时触发
 */
function handleTapCardButton(class_id: string) {
  router.push({
    name: 'managerChatList',
    params: { class_id },
  })
}
</script>

<template>
  <Spacer height="20" />
  <uv-list :custom-style="{ background: 'transparent' }">
    <view v-for="c in dataList" :key="c.id" :style="{ borderRadius: '20rpx', marginBottom: '20rpx' }">
      <view class="rounded-20rpx bg-white card-shadow">
        <RecordItem
          :picture="c.images!"
          :title="c.title || '未知标题'"
          :org="c.place || '未知'"
          :created-at="c.start_time || '未知时间'"
          hide-shadow
          show-divider
        />
        <view class="flex flex-row-reverse px-25rpx py-20rpx">
          <button
            class="h-60rpx w-160rpx flex items-center justify-center rounded-full bg-#0037AE text-24rpx text-white font-semibold leading-32rpx"
            hover-class="bg-#3266D9"
            @tap="handleTapCardButton(c.id.toString())"
          >
            点击查看
          </button>
        </view>
      </view>
    </view>
    <!-- 加载更多 -->
    <uv-load-more v-if="dataList.length > 3" :status="loadMoreStatus" @loadmore="loadMore" />
  </uv-list>
  <Spacer height="240" />
  <PageDefault v-if="dataList.length === 0">
    暂无班级
  </PageDefault>
  <uv-loading-page :loading="loading && !loadingMore" loading-text="加载中..." font-size="28rpx" loading-mode="spinner" />
</template>

<route lang="yaml">
name: 'managerChatClassList'
style:
  navigationBarTitleText: '选择管理班级'
</route>
